n = 3
m = 3
pos = (1,0)
matrix = [[0,1,2],[1,2,1],[0,1,2]]
signal = [False] * (len(matrix[0]) *  len(matrix))
for row in range(len(matrix)):
    for col in range(len(matrix[0])):
        if matrix[row][col] == 0:
            pos = row * len(matrix[0]) + col
            signal[pos] = True
            
time = 0
cur_towers = [[1,0]]
while True:
    if False not in signal:
        break
    
    
    for index in range(len(cur_towers)):
        
        cur_pos = cur_towers.pop(0) #当前位置坐标
        cur_row = cur_pos[0]
        cur_col = cur_pos[1]
        cur_index = cur_pos[0] * len(matrix[0]) + cur_pos[1]
        print(cur_row,cur_col,cur_index)
        
        if cur_row < 0 or cur_row >= len(matrix):
            continue
        
        if cur_col < 0 or cur_col >= len(matrix[0]):
            continue
        
        if signal[cur_index]:
            continue
        signal[cur_index] = True
        
        cur_towers.append([cur_row+1,cur_col])
        cur_towers.append([cur_row-1,cur_col])
        cur_towers.append([cur_row,cur_col+1])
        cur_towers.append([cur_row,cur_col-1])

        if matrix[cur_row][cur_row] == 1:
            time += 1
        elif matrix[cur_row][cur_row] == 2:
            time += 2
print(time)
    
        
        
        
    
    
    
    
    
    
print(signal)